<?php
class Shopping_Model_Category extends Zend_Db_Table{
	protected $_name = 'product_category';
	protected $_primary = 'id';
	
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options == null){
			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by'))
						 ->order('pc.order ASC');
			$result  = $db->fetchAll($select);
			
		}
		
		if($options['task'] == 'admin-edit'){
			$id = $arrParam['id'];
			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by'))
						 ->where('id != ?', $id, INTEGER )
						 ->order('pc.order ASC');
						 
			$result  = $db->fetchAll($select);
			
		}
		
		$system = new Zendvn_System_Recursive($result);
		$result = $system->buildArray(0);
		$tmp = array('id'=>0,'name'=>'Root category','level'=>1,'order'=>1,'parents'=>0);
		array_unshift($result,$tmp);
		
		return $result;
	} 
	public function countItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$ssFilter = $arrParam['ssFilter'];
		
		$select = $db->select()
					->from('user_group AS g',array('COUNT(g.id) AS totalItem'));
		
		if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('g.group_name LIKE ?',$keywords,STRING);
		}
		//echo $select;
		$result = $db->fetchOne($select);
		return $result;
		
	}
	
	public function sortItem($arrParam = null, $options = null){
			
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if(count($cid)>0){
			foreach ($cid as $key => $val) {
				$where = ' id = ' . $val;				
				$data = array('order'=>$order[$val]);
				$this->update($data,$where);
			}
		}
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
	
		if($options['task'] == 'admin-list'){

			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by'))
						 ->joinLeft('users AS u','u.id = pc.created_by',array('user_name'))
						 ->order('pc.order ASC');
			
			/*if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('g.group_name LIKE ?',$keywords,STRING);
			}*/
			//echo $select;
			$result  = $db->fetchAll($select);
			$system = new Zendvn_System_Recursive($result);
			$result = $system->buildArray(0);
			
		}
		return $result;
	
	}
	
	public function saveItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-add'){
			$info =  new Zendvn_System_Info();
			$created_by = $info->getMemberInfo('id');
			
			$row =  $this->fetchNew();
			$row->name 			= $arrParam['name'];
			$row->status 		= $arrParam['status'];
			$row->parents 		= $arrParam['parents'];
			$row->order 		= $arrParam['order'];
			$row->picture 		= $arrParam['picture'];
			
			$row->created 		= date("Y-m-d H:s:i");
			$row->created_by 	= $created_by;
			
			$row->save();
		}
		
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			
			$row =  $this->fetchRow($where);
			$row->name 			= $arrParam['name'];
			$row->status 		= $arrParam['status'];
			$row->parents 		= $arrParam['parents'];
			$row->order 		= $arrParam['order'];
			$row->picture 		= $arrParam['picture'];
			
			$row->modified 		= date("Y-m-d H:s:i");
			$row->modified_by 	= $created_by;
			
			$row->save();
		}
		
	}
	
	public function getItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		return $result;
	}
	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$db = Zend_Registry::get('connectDb');
			$id = $arrParam['id'];
			$select = $db->select()
					 	  ->from('product_category AS pc',array('id','name','status','parents','order','created_by'));
			$result  = $db->fetchAll($select);		
			$system = new Zendvn_System_Recursive($result);		
			$result = $system->buildArray($id);	 
			array_unshift($result,array('id'=> $id));
			
			foreach($result as $key => $val){				
				$where = ' id = ' . $val['id'];
				$this->delete($where);
			}
		
		}
		
		if($options['task'] == 'admin-multi-delete'){
			$cid = $arrParam['cid'];
		
			if(count($cid)>0){
				
				$db = Zend_Registry::get('connectDb');
				$id = $arrParam['id'];
				$select = $db->select()
						 	  ->from('product_category AS pc',array('id','name','status','parents','order','created_by'));
				$result  = $db->fetchAll($select);		
				
				$newArray = array();
				foreach ($cid as $key => $val){
					$id = $val;
					$newArray[] = array('id'=>$id);
					$system = new Zendvn_System_Recursive($result);		
					$tmp = $system->buildArray($id);
					foreach ($tmp as $keyTmp => $valTmp){
						$newArray[] = $valTmp;
					}
				}
				
				if(count($newArray)>0){
					foreach($newArray as $keyNew => $valNew){				
						$where = ' id = ' . $valNew['id'];
						$this->delete($where);
					}
				}
			}
		}
	}

	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}
		
	}
}